package com.kwedding.service.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.kwedding.bean.TB_Account;
import com.kwedding.bean.TB_Brand;
import com.kwedding.bean.TB_Customer;
import com.kwedding.dao.TB_AccountMapper;
import com.kwedding.dao.TB_BrandMapper;
import com.kwedding.dao.TB_CustomerMapper;
import com.kwedding.dto.web.RequestAccountStatistics;
import com.kwedding.dto.web.RequestWebCustomerList;
import com.kwedding.dto.web.RequestWebStatistics;
import com.kwedding.dto.web.ResponseStatistics;
import com.kwedding.dto.web.ResponseWebCustomerList;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyPageInfo;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.DateUtil;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.Resp_PageInfo;

@Service
public class WebCustomerService extends LoggerParent {

	@Autowired
	private TB_CustomerMapper tb_CustomerMapper;
	@Autowired
	private TB_AccountMapper tb_AccountMapper;
	@Autowired
	private TB_BrandMapper tb_BrandMapper;

	/**
	 * 获取顾客列表
	 * 
	 * @param webCustomerList
	 * @return
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebCustomerList> getCustomerList(RequestWebCustomerList webCustomerList) {
		CheckedUtil.isNull(webCustomerList.getCityId(), Constants.REQUEST_ERROR_1, "地区ID不能为空");
		int page = 1;
		if (webCustomerList.getPageNum() != null) {
			page = webCustomerList.getPageNum();
		}
		PageHelper.startPage(page, Constants.PAGESIZE);
		// 查询所有用户
		List<TB_Account> tb_Accounts = tb_AccountMapper.selectAccountListByCityId(null, false,
				webCustomerList.getCityId());
		List<Long> refIds = new ArrayList<>();
		tb_Accounts.stream().forEach(r -> {
			refIds.add(r.getAccountId());
		});
		List<TB_Customer> tb_Customers = tb_CustomerMapper.getCustomerListByRefIds(webCustomerList.getSearchName(),
				refIds, webCustomerList.getBrandId(), webCustomerList.getIsSuccess());
		List<ResponseWebCustomerList> responseWebCustomerLists = new ArrayList<>();
		if (tb_Customers != null && tb_Customers.size() > 0) {
			tb_Customers.stream().forEach(r -> {
				ResponseWebCustomerList customerList = CopyerObject.copyerObject(r, ResponseWebCustomerList.class);
				customerList.setIdStr(r.getCustomerId().toString());
				TB_Brand tb_Brand = tb_BrandMapper.selectByPrimaryKey(r.getBrandId());
				if (tb_Brand != null) {
					customerList.setBrandName(tb_Brand.getBrandName());
				}
				TB_Account tb_Account = tb_AccountMapper.selectbyPrimaryKey(r.getRefId());
				if (tb_Account != null) {
					customerList.setAccountName(tb_Account.getName());
				}
				responseWebCustomerLists.add(customerList);
			});
		}
		return CopyPageInfo.copyPageInfo(tb_Customers, responseWebCustomerLists);

	}

	/**
	 * 
	 * 获取订单统计图
	 * 
	 * @param requestWebStatistics
	 * @return
	 * @throws ParseException
	 */
	@Transactional(readOnly = true)
	public List<ResponseStatistics> getStatistics(RequestWebStatistics webStatistics) {
		CheckedUtil.isNull(webStatistics.getStartTime(), Constants.REQUEST_ERROR_1, "开始日期不能为空");
		CheckedUtil.isNull(webStatistics.getEndTime(), Constants.REQUEST_ERROR_2, "结束日期不能为空");
		CheckedUtil.isNull(webStatistics.getCityId(), Constants.REQUEST_ERROR_3, "地区ID不能为空");
		List<ResponseStatistics> webStatisticList = new ArrayList<>();
		List<TB_Account> tb_Accounts = tb_AccountMapper.selectAccountListByCityId(null, false,
				webStatistics.getCityId());
		List<Long> refIds = new ArrayList<>();
		tb_Accounts.stream().forEach(r -> {
			refIds.add(r.getAccountId());
		});
		System.out.println("用户ID" + refIds);
		List<Date> lDate = DateUtil.findDates(webStatistics.getStartTime(), webStatistics.getEndTime());
		for (int i = 0; i < lDate.size(); i++) {
			ResponseStatistics responseStatistics = new ResponseStatistics();
			if (i != lDate.size() - 1) {
				// 查询销售人员在是简时间段内的总客户数
				int count = tb_CustomerMapper.selectCustomerCountByRefIds(refIds, lDate.get(i), lDate.get(i + 1),
						webStatistics.getBrandId());
				// 查询销售人员在是简时间段内的成单数量
				int successCount = tb_CustomerMapper.getCustomerSuccessByRefIds(refIds, lDate.get(i), lDate.get(i + 1),
						webStatistics.getBrandId());
				// 走单数量
				int failedCount = count - successCount;
				responseStatistics.setSum(count);
				responseStatistics.setSuccessNum(successCount);
				responseStatistics.setFailNum(failedCount);
				responseStatistics.setDay(lDate.get(i));
				webStatisticList.add(responseStatistics);
			}
		}
		return webStatisticList;
	}

	/**
	 * 
	 * 获取接单统计
	 * 
	 * @param personStatistics
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ResponseStatistics> getStatisticsByStorePerson(RequestAccountStatistics accountStatistics) {
		CheckedUtil.isNull(accountStatistics.getAccountId(), Constants.REQUEST_ERROR_1, "门市人员不能为空");
		CheckedUtil.isNull(accountStatistics.getStartTime(), Constants.REQUEST_ERROR_2, "开始日期不能为空");
		CheckedUtil.isNull(accountStatistics.getEndTime(), Constants.REQUEST_ERROR_3, "结束日期不能为空");
		List<ResponseStatistics> webStatisticList = new ArrayList<>();
		List<Date> lDate = DateUtil.findDates(accountStatistics.getStartTime(), accountStatistics.getEndTime());
		for (int i = 0; i < lDate.size(); i++) {
			ResponseStatistics responseStatistics = new ResponseStatistics();
			if (i != lDate.size() - 1) {
				// 查询销售人员在是简时间段内的总客户数
				int count = tb_CustomerMapper.selectCustomerCount(accountStatistics.getAccountId(), lDate.get(i),
						lDate.get(i + 1), accountStatistics.getBrandId());
				// 查询销售人员在是简时间段内的成单数量
				int successCount = tb_CustomerMapper.getCustomerSuccess(accountStatistics.getAccountId(), lDate.get(i),
						lDate.get(i + 1), accountStatistics.getBrandId());
				// 走单数量
				int failedCount = count - successCount;
				responseStatistics.setSum(count);
				responseStatistics.setSuccessNum(successCount);
				responseStatistics.setFailNum(failedCount);
				responseStatistics.setDay(lDate.get(i));
				webStatisticList.add(responseStatistics);
			}
		}
		return webStatisticList;
	}
}
